//
//  ASHorizontalListNode.swift
//  glive
//
//  Created by zhu xietong on 2020/5/1.
//  Copyright © 2020 zhu xietong. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import Eelay

open class ASHorizontalKit:ASSizeRangeCell {
    open class func initCollectionNode() -> (ASCollectionNode,UICollectionViewFlowLayout){
        let flowLayout = UICollectionViewFlowLayout()
        let collectionNode = ASCollectionNode(collectionViewLayout: flowLayout)
        flowLayout.minimumLineSpacing = 4
        flowLayout.minimumInteritemSpacing = 4
        flowLayout.sectionInset = .zero
        
        flowLayout.scrollDirection = .horizontal
        
        collectionNode.view.backgroundColor = .clear
        collectionNode.view.backgroundView?.backgroundColor = .clear
        collectionNode.showsHorizontalScrollIndicator = false
        collectionNode.showsVerticalScrollIndicator = false
        collectionNode.alwaysBounceHorizontal = true
        return (collectionNode,flowLayout)
    }

    open class var collectionNodeSize:CGSize{
        get{
            return CGSize(width: Swidth, height: Swidth)
        }
    }
    
}

open class ASHorizontalListNode<Kit:ASHorizontalKit>:JoCellNode,ASCollectionVConfig,EeActionDelegate,ASCollectionDelegateFlowLayout{
    open func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        delegate?.touch(cell: cell, actionID: actionID, model: model)
    }
    
    open func collectionNode(_ collectionNode: ASCollectionNode, didSelectItemAt indexPath: IndexPath) {
//        guard let model = collectionNode.cellData(at: indexPath) else {
//            return
//        }
    }
    
    
    public func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
        return collectionNode.cellBlock(indexPath: indexPath, fillModel: true, delegate: self) { (_) in
            
        }.block
//        return collectionNode.cellBlock(indexPath: indexPath, nodeBuild: ({_ in})).block
    }
    
    @objc open func collectionNode(_ collectionNode: ASCollectionNode, sizeRangeForHeaderInSection section: Int) -> ASSizeRange {
        return ASSizeRange(min: .zero, max: .zero)
    }
    
    
    public var collectionNode:ASCollectionNode!
    public var flowLayout:UICollectionViewFlowLayout
    
    public var sections:TP.section{
        get{
            let sec:TP.section = [
                []
            ]
            return sec
        }
    }
    
    public var selector:TP.selector = [
        "<section0>":Kit.self
    ]
    
    
    
    public override init() {
        let initObj = Kit.initCollectionNode()
        flowLayout = initObj.1
        collectionNode = initObj.0
        super.init()
    

        configCollections(nodes: collectionNode)
        
        self.collectionNode.clipsToBounds = false
        self.clipsToBounds = false
       
    }
    public var listNode = "list"
    
    open override func mo_model_reload() {
        
        collectionNode.load(sections: sections, selector: selector)
        let list = DataNode.path(node: listNode, mo.model, value: NSMutableArray())
        collectionNode.model <-- ("section0",list,NodeInsert.arrayRefresh)
        co_delay(delay: 0.1) {[weak self] in
            self?.collectionNode.reloadData()
        }        
        self.setNeedsLayout()
    }
    

    
    open override func specGetter() -> _Spec {
//        let kit = KitNode().sizeRange().min.height
//        let titleBar:_Spec = .stack([[0,0,0,0],"h",&-.center,_justfy(.space_between)], childs: [
//            titleN.Spec,
//            moreN.Spec
//            ])
        
        let size = Kit.collectionNodeSize
//        let spec:_Spec = .stack(["h",&-.stretch,], childs: [
//            collectionNode["\(size.height)",size.width]
//            ])
        return .stack(["h"], childs: [
            collectionNode["\(size.height)",size.width]
        ])
    }
    
    
    public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
    }
}


open class ASHorizontaltNode<Kit:ASHorizontalKit>:MoDisplayNode,ASCollectionVConfig,EeActionDelegate,ASCollectionDelegateFlowLayout{
    open func touch(cell: Any, actionID: String, model: NSMutableDictionary) {}
    
    open func collectionNode(_ collectionNode: ASCollectionNode, didSelectItemAt indexPath: IndexPath) {
//        guard let model = collectionNode.cellData(at: indexPath) else {
//            return
//        }
    }
    
    
    public func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
        return collectionNode.cellBlock(indexPath: indexPath, nodeBuild: ({_ in})).block
    }
    
    @objc open func collectionNode(_ collectionNode: ASCollectionNode, sizeRangeForHeaderInSection section: Int) -> ASSizeRange {
        return ASSizeRange(min: .zero, max: .zero)
    }
    
    
    
    public var collectionNode:ASCollectionNode!
    public var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    public var sections:TP.section{
        get{
            let sec:TP.section = [
                []
            ]
            return sec
        }
    }
    
    public var selector:TP.selector = [
        "<default>":Kit.self
    ]
    

    
    
    open override func willLayoutRules() {
    
        let initObj = Kit.initCollectionNode()
        flowLayout = initObj.1
        collectionNode = initObj.0

        configCollections(nodes: collectionNode)
        self.collectionNode.clipsToBounds = false
        self.clipsToBounds = false
       
    }
    public var listNode = "list"
    
    open override func mo_model_reload() {
        collectionNode.mo_controller = mo.controller
        collectionNode.load(sections: sections, selector: selector)
        let list = DataNode.path(node: listNode, mo.model, value: NSMutableArray())
        collectionNode.model <-- ("section0",list,NodeInsert.arrayRefresh)
        co_delay(delay: 0.05) {[weak self] in
            self?.collectionNode.reloadData()
        }
        self.setNeedsLayout()
    }
    

    
    open override func specGetter() -> _Spec {
//        let kit = KitNode().sizeRange().min.height
//        let titleBar:_Spec = .stack([[0,0,0,0],"h",&-.center,_justfy(.space_between)], childs: [
//            titleN.Spec,
//            moreN.Spec
//            ])
        
        let size = Kit.collectionNodeSize
//        let spec:_Spec = .stack(["h",&-.stretch,], childs: [
//            collectionNode["\(size.height)",size.width]
//            ])
        return .stack(["h"], childs: [
            collectionNode["\(size.height)",size.width]
        ])
    }
    
    
    public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
    }
}

